package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.junit.Test;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class SQLcode2 {


    @Test
    public void selectOne() throws SQLException {
        // 1. 创建QueryRunner
        QueryRunner qr  = new QueryRunner(DruidUtils.getDataSource());

        // 2.编写SQL
        String sql = "SELECT * FROM phone WHERE price > 2000 AND prodate < '2019-01-01'";

        // 3.执行查询
        List<Object[]> query = qr.query(sql, new ArrayListHandler());

        // 4.遍历集合获取数据
        for (Object[] objects : query){
            System.out.println(Arrays.toString(objects));
        }
    }

    @Test
    public void selectTwo() throws SQLException {
        // 1. 创建QueryRunner
        QueryRunner qr  = new QueryRunner(DruidUtils.getDataSource());

        // 2.编写SQL
        String sql = "  SELECT * FROM phone WHERE color ='白色'";

        // 3.执行查询
        List<Object[]> query = qr.query(sql, new ArrayListHandler());

        // 4.遍历集合获取数据
        for (Object[] objects : query){
            System.out.println(Arrays.toString(objects));
        }

    }
}
